package cn.steam.config;

import cn.steam.service.TokenService;
import cn.steam.xo.TokenVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author zhang jintao on 2022/08/01
 **/
@SuppressWarnings("NullableProblems")
@Component
public class LoginInterceptor extends HandlerInterceptorAdapter {

    @Autowired
    TokenService tokenService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String token = request.getHeader("token");
        TokenVO vo = new TokenVO();
        vo.setToken(token);
        tokenService.set(vo);
        return super.preHandle(request, response, handler);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        tokenService.remove();
        super.afterCompletion(request, response, handler, ex);
    }
}
